package Lab2;

import java.util.Scanner;

public class Rational implements Comparable {
	private int numerable;
	private int denominator;
	public Rational(){
		this.numerable = 0;
		this.denominator = 0;
	}
	public Rational(int tuso, int mauso){
		tuso = this.numerable;
		mauso = this.denominator;
	}
	public int getTuso() {
		return numerable;
	}
	public void setTuso(int tuso) {
		this.numerable = tuso;
	}
	public int getMauso() {
		return denominator;
	}
	public void setMauso(int mauso) {
		this.denominator = mauso;
	}
	public void input(){
		Scanner input = new Scanner(System.in);
		System.out.println("Nhap tu so: ");
		this.numerable = input.nextInt();
		while(this.denominator == 0){
			System.out.println("Nhap mau so: ");
			this.denominator = input.nextInt();
		}
	}

	public int UCLN(int a, int b){
		if(a == 0 || b == 0){
			return a+b;
		}
		while(a != b){
			if(a > b){
				a = a-b;
			}
			else {
				b = b-a;
			}
		}
		return a;
	}
	public void reduce(){
		int i = UCLN(numerable,denominator);
		this.numerable = this.numerable /i;
		this.denominator = this.denominator/i;
	}
	public Rational reciprocal(){
		Rational ps = new Rational();
		ps.numerable = this.denominator;
		ps.denominator = this.numerable;
		ps.reduce();
		return ps;
	}
	public Rational add(Rational f){
		Rational ps = new Rational();
		ps.numerable = this.numerable * f.denominator + this.denominator * f.numerable;
		ps.denominator = this.denominator * f.denominator;
		ps.reduce();
		return ps;
	}
	public Rational subtract(Rational f){
		Rational ps = new Rational();
		ps.numerable = this.numerable * f.denominator - this.denominator * f.numerable;
		ps.denominator = this.denominator * f.denominator;
		ps.reduce();
		return ps;
	}
	public Rational multiply(Rational f){
		Rational ps = new Rational();
		ps.numerable = this.numerable * f.numerable;
		ps.denominator = this.denominator * f.denominator;
		ps.reduce();
		return ps;
	}
	
	public Rational divide(Rational f){
		Rational ps = new Rational();
		ps.numerable = this.numerable * f.denominator;
		ps.denominator = this.denominator * f.numerable;
		ps.reduce();
		return ps;
	}
	public void output(String s){
		if(this.numerable !=  this.denominator){
			if(this.numerable !=0){
				System.out.println(s + " " + this.numerable + "/" + this.denominator);
			}
			else{
				System.out.println(s + "0");
			}
		}
		else{
			System.out.println( s + "1");
		}
	}
	
	public int compareTo(Object o) {
		Rational rat = (Rational)o;
		double ps1 = this.numerable/this.denominator;
		double ps2 = rat.numerable/rat.denominator;
		if(Math.abs(ps1-ps2) < 0.0001){
			System.out.println("Nho hon");
			return 0;
		}
		else{
			System.out.println("Lon hon");
			return 1;
		}
		
	}
	/* Cach khac
	 int compareTo(Object o){
	 Rational = (Rational)o;
	 }
	 */
	
	public static void main(String[] arg){
		Rational ps1 = new Rational();
		Rational ps2 = new Rational();
		ps1.input();
		ps2.input();
		ps1.reciprocal().output("Nghich dao: ");;
		ps1.add(ps2).output("Tong: ");;
		ps1.subtract(ps2).output("Hieu : ");;
		ps1.multiply(ps2).output("Tich: ");
		ps1.divide(ps2).output("Thuong: ");;
		ps1.compareTo(ps2);
	
	}
	
}
